﻿using UnityEngine;
using System.Collections;

public class PlayerShooting : MonoBehaviour
{
	public float maximumDamage = 120f;     
	public float minimumDamage = 45f;
	public float flashIntensity = 3f;
	public float fadeSpeed = 10f;
	public float timer = 100.0f;
	public float raycastDistance;
	public float reloadSpeed;

	public AnimationClip shooting;

	private LineRenderer laserShotLine;                 
	private Light laserShotLight;                       
	private Transform player;                           
	private bool isShooting;                            
	private Transform cameraTransform;
	private bool ableShoot=true;
	private new Animation animation;
	
	
	void Awake ()
	{
		// Setting up the references.
		laserShotLine = GetComponentInChildren<LineRenderer>();
		laserShotLight = laserShotLine.gameObject.light;
		player = GameObject.FindGameObjectWithTag("Player").transform;
		
		// The line renderer and light are off to start.
		laserShotLine.enabled = false;
		laserShotLight.intensity = 0f;
		animation = GetComponent<Animation> ();
		cameraTransform = GameObject.FindGameObjectWithTag ("MainCamera").transform;
	}
	
	
	void Update ()
	{
		isShooting = false;
		laserShotLine.enabled = false;

			if(Input.GetKey(KeyCode.LeftControl) && ableShoot)
			{
				animation.Play(shooting.name);
				float shot = 0.6f;

				if(shot > 0.5f && !isShooting)
				{	
					Shoot();
				}
				laserShotLight.intensity = Mathf.Lerp(laserShotLight.intensity, 0f, fadeSpeed * Time.deltaTime);
				timer--;
				
			}
			else
			{
				laserShotLight.intensity = 0.0f;
			}

		if(timer<=0)
		{
			ableShoot = false;
		}

			
		if(!ableShoot)
		{
			timer+=reloadSpeed;
			if(timer>=100)
			{
				ableShoot= true;
			}
		}

		//timer++;
		
	}
	
	void Shoot ()
	{
		isShooting = true;

		Vector3 fwd = transform.TransformDirection(Vector3.forward);

		Vector3 test = transform.position;
		test.y += 1.5f;

		RaycastHit[] hits = Physics.RaycastAll (test, transform.forward, raycastDistance);

		for (int i = 0 ; i < hits.Length ; ++i)
		{
			if (hits[i].transform.tag == "Enemy" && hits[i].transform.gameObject.GetComponent<SphereCollider>())
			{
				if(i + 1 <= hits.Length-1)
				{
					if(hits[i+1].transform.gameObject.GetComponent<SphereCollider>() )
					{
						HealthPickup health = hits[i+1].transform.gameObject.GetComponent<HealthPickup>();
						if(health)
						{
							health.TakeDamage(100.0f);
							if(!health.IsAlive())
							{
								EnemyAI explosion = hits[i+1].transform.gameObject.GetComponent<EnemyAI>();
								explosion.Explodes(hits[i+1].distance);
							}
						}
					}
				}
			}
			else if(hits[i].transform.CompareTag("Target"))
			{
				Debug.Log("Hits Target");
				TargetHit target = hits[i].transform.GetComponent<TargetHit>();
				target.bExists = false;

			}
		}

		/*if (Physics.Raycast(test, transform.forward, out hit, raycastDistance)) // change the position so it can be the same as the end of the laser
		{
			Debug.DrawRay(test, transform.forward);
			if(hit.transform.tag == "Target")
			{
				Debug.Log("Hits Target");
			}
			else if (hit.transform.tag == "Enemy")
			{
				Debug.Log("Hits");
				HealthPickup health = hit.transform.gameObject.GetComponent<HealthPickup>();
				if(health)
				{
					health.TakeDamage(100.0f);
					if(!health.IsAlive())
					{
						EnemyAI explosion = hit.transform.gameObject.GetComponent<EnemyAI>();
						explosion.Explodes(hit.distance);
					}
				}
			}

		}
		else
		{
			Debug.Log("Didnt hit!!!!");
		}*/
		ShotEffects();
	}
	
	
	void ShotEffects ()
	{
		laserShotLine.SetPosition(0, laserShotLine.transform.position);
		laserShotLine.SetPosition(1, player.position + Vector3.up * 1.5f);
		laserShotLine.enabled = true;
		laserShotLight.intensity = flashIntensity;

	}
}